package com.strangebreeds.therefromhere;

import java.util.Properties;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.MapMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

@MessageDriven(activationConfig = { @ActivationConfigProperty(propertyName = "destination", propertyValue = "topic/tfh_status_topic"),
									@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic") })
public class StatusBean implements MessageListener
{
	private Logger logger = Logger.getLogger(StatusBean.class);

	TopicConnection conn = null;
	TopicSession session = null;
	Topic topic = null;

	{
		org.apache.log4j.PropertyConfigurator.configure("log4j.properties");
		logger.setLevel(Level.DEBUG);
	}

	public void onMessage(Message inMessage)
	{
		MapMessage msg = null;

		try
		{
			if (inMessage instanceof MapMessage)
			{
				msg = (MapMessage) inMessage;
				logger.debug("Job ID " + msg.getInt("jobID") + " is working on "
						+ msg.getString("pageName") + " which is type " + msg.getString("pageType"));
			}
		}
		catch (JMSException jmse)
		{
			logger.error("JMSException: " + jmse.getMessage(), jmse);
		}
	}

	private void setupPubSub() throws JMSException, NamingException
	{
		Properties properties = new Properties();
		properties.put(	Context.INITIAL_CONTEXT_FACTORY,
						"org.jnp.interfaces.NamingContextFactory");
		properties.put(	Context.URL_PKG_PREFIXES,
						"org.jboss.naming:org.jnp.interfaces");
		properties.put(Context.PROVIDER_URL, "localhost:1099");
		Context iniCtx = new InitialContext(properties);

		// InitialContext iniCtx = new InitialContext();
		Object tmp = iniCtx.lookup("ConnectionFactory");
		TopicConnectionFactory tcf = (TopicConnectionFactory) tmp;
		conn = tcf.createTopicConnection();
		topic = (Topic) iniCtx.lookup("topic/tfh_status_topic");
		session = conn.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
		conn.start();
	}

	public void stop() throws JMSException
	{
		conn.stop();
		session.close();
		conn.close();
	}

	public StatusBean()
	{
		try
		{
			setupPubSub();
		}
		catch (JMSException jmse)
		{
			logger.error("Caught a JMSException: " + jmse.getMessage(), jmse);
		}
		catch (NamingException ne)
		{
			logger.error("Caught a NamingException: " + ne.getMessage(), ne);
		}
	}

	public void sendPageName(Integer jobID, PageType pageType, String pageName)
	{
		try
		{
			// Send a map message with the properties (at this moment, the
			// job ID and the text)
			MapMessage mm = session.createMapMessage();
			mm.setInt("jobID", jobID);
			mm.setString("pageType", pageType.getValue());
			mm.setString("pageName", pageName);

			TopicPublisher send = session.createPublisher(topic);

			send.publish(mm);

			send.close();
		}
		catch (JMSException jmse)
		{
			logger.error("Caught a JMSException: " + jmse.getMessage(), jmse);
		}
	}
}
